package servlet;

import db.DB;
import model.User;
import model.permission;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/login")//访问的时候给一个地址,高于3.0版本的包才能这么配地址
public class login extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setHeader("content-type","text/html;charset=utf-8");
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        HttpSession session=req.getSession();
        DB dbtest=new DB();
        User user= dbtest.login(username,password);
        String message = null; //定义message变量
        if(user !=null){
                session.setAttribute("user", user);
                resp.sendRedirect("index.jsp");
        }
        else {
            resp.getWriter().print("<script language='javascript'>" +
                    "alert('您输入的账号或密码错误，请重新输入！');" +
                    "window.location.href='login.jsp';</script>')");
//            message = "账号错误！！";
//            session.setAttribute("message",message);
//            resp.sendRedirect("login.jsp");
        }
    }

}
